package com.xiaoq.order.service.impl;

import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.order.entity.OrderTourPipe;
import com.xiaoq.order.entity.PayState;
import com.xiaoq.order.entity.ShopOrderEntity;
import com.xiaoq.order.repository.IShopOrderRepository;
import com.xiaoq.order.service.IShopOrderService;
import com.xiaoq.order.vo.OrderQueryVo;
import com.xiaoq.order.vo.OrderTourPipeQueryVo;
import com.xiaoq.shop.service.IUserCouponService;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjh on 2017/11/7.
 */
@Service
public class ShopOrderServiceImpl implements IShopOrderService {

    @Autowired
    private IShopOrderRepository shopOrderRepository;

    @Autowired
    private IUserCouponService userCouponService;

    @Override
    public void saveShopOrder(ShopOrderEntity shopOrderEntity) {
        shopOrderRepository.save(shopOrderEntity);
    }

    @Override
    public ShopOrderEntity getShopOrderById(String shopOrderId, String userId) {
        return shopOrderRepository.getShopOrder(shopOrderId, userId);
    }

    @Override
    public ShopOrderEntity getShopOrderByOrderId(String orderId) {
        return shopOrderRepository.getShopOrder(orderId);
    }

    @Override
    public boolean cancelOrder(String shopOrderId, String userId) {
        boolean cancelOrderFlag = false;
        if(StringUtils.isAnyBlank(shopOrderId, userId)){
            return cancelOrderFlag;
        }
        ShopOrderEntity shopOrderEntity = shopOrderRepository.getShopOrder(shopOrderId, userId);
        if(shopOrderEntity==null || StringUtils.isBlank(shopOrderEntity.getId())){
            return cancelOrderFlag;
        }

        //待付款的才能取消订单
        if(shopOrderEntity.getPayState() == PayState.NOPAYMENT.value()
                && shopOrderEntity.getCanceledTime()==null){
            cancelOrderFlag = shopOrderRepository.cancelShopOrder(shopOrderId, userId);
        }

        //修改优惠券状态
        if(cancelOrderFlag && StringUtils.isNotBlank(shopOrderEntity.getUserCounponId())){
            userCouponService.releaseUserCoupon(shopOrderEntity.getUserCounponId(),userId);
        }

        return cancelOrderFlag;
    }

    @Override
    public List<ShopOrderEntity> getUserAllOrderByUserId(String userId, Integer payState, String userDeleteFlag,Integer orderType, Integer orderState) {
        if(StringUtils.isBlank(userId)){
            return new ArrayList<>();
        }
        ShopOrderEntity shopOrderEntity = new ShopOrderEntity();
        shopOrderEntity.setUserId(userId);
        shopOrderEntity.setPayState(payState);
        shopOrderEntity.setUserDeleteFlag(userDeleteFlag);
        shopOrderEntity.setOrderState(orderState);
        shopOrderEntity.setOrderType(orderType);
        return shopOrderRepository.getUserAllOrder(shopOrderEntity);
    }

    @Override
    public List<ShopOrderEntity> getInterpreterAllOrderByInterId(String interpreterId) {
        return shopOrderRepository.getInterpreterAllOrder(interpreterId);
    }

    @Override
    public boolean updateShopOrderByOrderId(ShopOrderEntity shopOrder) {
        return shopOrderRepository.updateByOrderId(shopOrder);
    }

    @Override
    public int updateReviewStateInOrder(String userId, String orderId, String skuCode, int reviewState, int orderState) {
        return shopOrderRepository.updateReviewStateInOrder(userId, orderId, skuCode, reviewState, orderState);
    }

    @Override
    public void callBackReviewState(String userId, String shopOrderId, String skuCode, int orderState) {
        if(!StringUtils.isAnyBlank(userId, shopOrderId, skuCode)){
            shopOrderRepository.callBackReviewState(userId, shopOrderId, skuCode, orderState);
        }
    }

    @Override
    public boolean deleteUserOrderByUserIdAndOrderId(String userId, String shopOrderId) {
        if(StringUtils.isAnyBlank(userId, shopOrderId)){
            return false;
        }
        ShopOrderEntity shopOrderEntity = new ShopOrderEntity();
        shopOrderEntity.setUserId(userId);
        shopOrderEntity.setShopOrderId(shopOrderId);
        shopOrderEntity.setUserDeleteFlag(ConstantUtil.DELETE_STATUS);
        return shopOrderRepository.deleteOrder(shopOrderEntity);
    }

    @Override
    public boolean deleteInterOrderByInterIdAndOrderId(String interId, String shopOrderId) {
        if(StringUtils.isAnyBlank(interId, shopOrderId)){
            return false;
        }
        ShopOrderEntity shopOrderEntity = new ShopOrderEntity();
        shopOrderEntity.setInterpreterId(interId);
        shopOrderEntity.setShopOrderId(shopOrderId);
        shopOrderEntity.setInterDeleteFlag(ConstantUtil.DELETE_STATUS);
        return shopOrderRepository.deleteOrder(shopOrderEntity);
    }

    @Override
    public List<ShopOrderEntity> getInterpreterOrderList(String interpreterId, Integer orderState, Integer payState) {
        return shopOrderRepository.getInterpreterAllOrder(interpreterId, orderState, payState);
    }

    @Override
    public List<ShopOrderEntity> getNotPayExpiredShopOrderList(String shopId, Integer orderType) {
        return shopOrderRepository.getNotPayExpiredShopOrderList(shopId, orderType);
    }

    @Override
    public List<ShopOrderEntity> getShopAllOrders(OrderQueryVo orderQueryVo, PagingVo pagingVo) {
        if(orderQueryVo==null || StringUtils.isBlank(orderQueryVo.getShopId())){
            return new ArrayList<>();
        }

        return shopOrderRepository.getShopAllOrders(orderQueryVo, pagingVo);
    }

    @Override
    public long getShopAllOrdersCount(OrderQueryVo orderQueryVo, PagingVo pagingVo) {
        if(orderQueryVo==null || StringUtils.isBlank(orderQueryVo.getShopId())){
            return 0L;
        }

        return shopOrderRepository.getShopAllOrdersCount(orderQueryVo, pagingVo);
    }

    @Override
    public List<ShopOrderEntity> getInterOrderList(String shopId, Integer payStatus, List<String> interList) {
        List<ShopOrderEntity> resultList = new ArrayList<>();
        if(StringUtils.isBlank(shopId)){
            return resultList;
        }
        OrderQueryVo orderQueryVo = new OrderQueryVo();
        orderQueryVo.setShopId(shopId);
        orderQueryVo.setInterList(interList);
        orderQueryVo.setPayState(payStatus);
        return shopOrderRepository.getShopAllOrders(orderQueryVo, null);
    }

    @Override
    public List<ShopOrderEntity> getShopOrderList(String shopId, List<String> couponCodeList) {
        return shopOrderRepository.getShopOrderList(shopId, couponCodeList);
    }

    @Override
    public List<OrderTourPipe> getShopAllOrders4Tour(OrderTourPipeQueryVo orderQueryVo, PagingVo pagingVo) {
        return shopOrderRepository.getShopAllOrders4Tour(orderQueryVo, pagingVo);
    }

    @Override
    public long getShopAllOrders4TourCount(OrderTourPipeQueryVo orderQueryVo) {
        return shopOrderRepository.getShopAllOrders4TourCount(orderQueryVo);
    }

    @Override
    public List<ShopOrderEntity> getPaidNotUserFromOrders(Integer limit) {
        return shopOrderRepository.getPaidNotUserFromOrders(limit);
    }
}
